const router = require("koa-router")();
const fs = require("fs");

router.get("/getFileList", async (ctx, next) => {
  try {
    let path = `${__dirname.replace("\\routes", "")}\\public\\temporary\\`;
    let json = fs
      .readdirSync(path)
      .map(item => {
        let _path = `${path}${item}`;
        let stat = fs.statSync(_path);
        stat.name = item;
        return stat;
      }).filter(item => {
        return item.isFile();
      });
    ctx.set("Content-Type", "application/json");
    return (ctx.body = JSON.stringify(json));
  } catch (error) {
    return (ctx.body = `获取失败${error}`);
  }
});

module.exports = router;
